package org.wtframework.platform.server.model.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Proxy;
import org.openredhead.springx.core.server.model.IRole;

@Entity
@Proxy(lazy = true)
@DynamicInsert(value = true)
@DynamicUpdate(value = true)
@Table(name = "orh_sx_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role implements IRole {

	private static final long serialVersionUID = 3093895260434908184L;

	private Integer id;

	private String name;
	
	private boolean superRole = false; //是否超级角色，是则具有所有权限

	private String descn;
	
	private boolean enabled = true;

	private Date establishDate = new Date(); //创建日期

	private List<Integer> resourcesId; //资源权限列表
	
	private Integer orderId;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	@Column(name = "order_id")
	public Integer getOrderId() {
		return orderId;
	}

	@Column(name = "is_enabled")
	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "descn")
	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@Column(name = "establish_date")
	public Date getEstablishDate() {
		return establishDate;
	}

	public void setEstablishDate(Date establishDate) {
		this.establishDate = establishDate;
	}

	@Column(name = "is_super_role")
	public boolean isSuperRole() {
		return superRole;
	}

	public void setSuperRole(boolean superRole) {
		this.superRole = superRole;
	}

	@ElementCollection(fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	@JoinTable(name = "orh_sx_link_res_role", joinColumns = @JoinColumn(name = "role_id"))
	@Column(name = "res_id")
	public List<Integer> getResourcesId() {
		return resourcesId;
	}

	public void setResourcesId(List<Integer> resourcesId) {
		this.resourcesId = resourcesId;
	}

	@Override
	public boolean equals(Object o) {

		if (o == null) return false;

		if (!(o instanceof Role)) return false;

		final Role instance = (Role) o;

		if (getId() != null ? !getId().equals(instance.getId()) : instance.getId() != null) return false;

		return true;

	}

	@Override
	public int hashCode() {

		return (getId() != null ? getId().hashCode() : 0);

	}

}
